﻿using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using Microsoft.CSharp.RuntimeBinder;

namespace YArchitech.Temp
{
	internal sealed class FormUtils
	{
		public static bool SelectDataGridRow(DataGridView grid, DataGridViewRow rowToSelect)
		{
			foreach (object obj in grid.SelectedRows)
			{
				((DataGridViewRow)obj).Selected = false;
			}
			rowToSelect.Selected = true;
			grid.FirstDisplayedScrollingRowIndex = rowToSelect.Index;
			return true;
		}

		public static void DeleteAllRows(DataGridView grid)
		{
			for (int i = grid.Rows.Count - 1; i > -1; i--)
			{
				grid.Rows.RemoveAt(i);
			}
			grid.Rows.Clear();
		}

		public static string GetCellText(DataGridView grid, int col, int row)
		{
			object value = grid.Rows[row].Cells[col].Value;
			if (value == null)
			{
				return "";
			}
			return value.ToString();
		}

		public static string GetCellText(DataGridViewCell cell)
		{
			object value = cell.Value;
			if (value == null)
			{
				return "";
			}
			return value.ToString();
		}

		public static void SetCellText(DataGridViewCell cell, dynamic newValue)
		{
			if (cell == null)
			{
				return;
			}
            //if (FormUtils.<>o__6.<>p__1 == null)
            //{
            //    FormUtils.<>o__6.<>p__1 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(FormUtils)));
            //}
            //Func<CallSite, object, string> target = FormUtils.<>o__6.<>p__1.Target;
            //CallSite <>p__ = FormUtils.<>o__6.<>p__1;
            //if (FormUtils.<>o__6.<>p__0 == null)
            //{
            //    FormUtils.<>o__6.<>p__0 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(FormUtils), new CSharpArgumentInfo[]
            //    {
            //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
            //    }));
            //}
            //string text = target(<>p__, FormUtils.<>o__6.<>p__0.Target(FormUtils.<>o__6.<>p__0, newValue));
            //if (newValue is double)
            //{
            //    if (FormUtils.<>o__6.<>p__3 == null)
            //    {
            //        FormUtils.<>o__6.<>p__3 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(FormUtils)));
            //    }
            //    Func<CallSite, object, string> target2 = FormUtils.<>o__6.<>p__3.Target;
            //    CallSite <>p__2 = FormUtils.<>o__6.<>p__3;
            //    if (FormUtils.<>o__6.<>p__2 == null)
            //    {
            //        FormUtils.<>o__6.<>p__2 = CallSite<Func<CallSite, object, string, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(FormUtils), new CSharpArgumentInfo[]
            //        {
            //            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
            //            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null)
            //        }));
            //    }
            //    text = target2(<>p__2, FormUtils.<>o__6.<>p__2.Target(FormUtils.<>o__6.<>p__2, newValue, "G0"));
            //    if (text.IndexOf(FormUtils.DECIMAL_POINT) > 0)
            //    {
            //        if (FormUtils.<>o__6.<>p__6 == null)
            //        {
            //            FormUtils.<>o__6.<>p__6 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(FormUtils)));
            //        }
            //        Func<CallSite, object, string> target3 = FormUtils.<>o__6.<>p__6.Target;
            //        CallSite <>p__3 = FormUtils.<>o__6.<>p__6;
            //        if (FormUtils.<>o__6.<>p__5 == null)
            //        {
            //            FormUtils.<>o__6.<>p__5 = CallSite<Func<CallSite, object, char[], object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "TrimEnd", null, typeof(FormUtils), new CSharpArgumentInfo[]
            //            {
            //                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
            //                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null)
            //            }));
            //        }
            //        Func<CallSite, object, char[], object> target4 = FormUtils.<>o__6.<>p__5.Target;
            //        CallSite <>p__4 = FormUtils.<>o__6.<>p__5;
            //        if (FormUtils.<>o__6.<>p__4 == null)
            //        {
            //            FormUtils.<>o__6.<>p__4 = CallSite<Func<CallSite, object, string, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(FormUtils), new CSharpArgumentInfo[]
            //            {
            //                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
            //                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null)
            //            }));
            //        }
            //        text = target3(<>p__3, target4(<>p__4, FormUtils.<>o__6.<>p__4.Target(FormUtils.<>o__6.<>p__4, newValue, "f3"), FormUtils.trimEnd));
            //        text = text.TrimEnd(new char[]
            //        {
            //            FormUtils.DECIMAL_POINT
            //        });
            //    }
            //}
            //cell.Value = text;
		}

		public static void MoveControlToCell(System.Windows.Forms.Control ctrl, DataGridView grid, int col, int row)
		{
			System.Drawing.Rectangle cellDisplayRectangle = grid.GetCellDisplayRectangle(col, row, false);
			FormUtils.MoveWindow(ctrl, cellDisplayRectangle);
		}

		public static void MoveWindow(System.Windows.Forms.Control ctrl, System.Drawing.Rectangle rect)
		{
			ctrl.Location = rect.Location;
			ctrl.Width = rect.Width;
			ctrl.Height = rect.Height;
		}

		public static System.Drawing.Rectangle GetCellDisplayRectangular(DataGridViewCell cell)
		{
			if (cell.DataGridView == null)
			{
				return System.Drawing.Rectangle.Empty;
			}
			return cell.DataGridView.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, false);
		}

		public static int GetImageIndex(object comp)
		{
			if (comp is HYDuctSystem)
			{
				return 0;
			}
			if (comp is HYBranch)
			{
				return 1;
			}
			if (comp is HYDuctStraight)
			{
				return 2;
			}
			return -1;
		}

		private static char DECIMAL_POINT = '.';

		private static char[] trimEnd = new char[]
		{
			'0'
		};
	}
}
